#!/usr/bin/env python3
"""
初始化演示数据脚本
为AI学习系统创建必要的演示数据
"""

import json
import os
from datetime import datetime
from pathlib import Path

def init_demo_data():
    """初始化演示数据"""
    
    # 创建数据目录
    data_dir = Path("data")
    data_dir.mkdir(exist_ok=True)
    
    # 创建演示规则文件
    demo_rules = [
        {
            "rule_set_id": "demo-001",
            "building_type": "commercial",
            "total_rules": 3,
            "created_at": datetime.now().isoformat(),
            "rules": [
                {
                    "rule_id": "fire_door_width_demo",
                    "category": "消防安全",
                    "title": "防火门净宽度检查",
                    "description": "疏散门净宽度不应小于1.4米",
                    "threshold": 1.4,
                    "unit": "m",
                    "severity": "严重",
                    "standard": "GB 50016-2014 建筑设计防火规范"
                },
                {
                    "rule_id": "office_height_demo",
                    "category": "功能空间",
                    "title": "办公室净高检查",
                    "description": "办公室净高不应低于2.6米",
                    "threshold": 2.6,
                    "unit": "m",
                    "severity": "中等",
                    "standard": "JGJ 67-2006 办公建筑设计规范"
                },
                {
                    "rule_id": "corridor_width_demo",
                    "category": "消防安全",
                    "title": "疏散走道宽度检查",
                    "description": "疏散走道净宽度不应小于1.4米",
                    "threshold": 1.4,
                    "unit": "m",
                    "severity": "严重",
                    "standard": "GB 50016-2014 建筑设计防火规范"
                }
            ],
            "metadata": {
                "context_used": False,
                "optimization_level": "standard",
                "source": "AI规则引擎演示"
            }
        }
    ]
    
    # 保存演示规则
    rules_file = data_dir / "ai_rules.json"
    with open(rules_file, 'w', encoding='utf-8') as f:
        json.dump(demo_rules, f, ensure_ascii=False, indent=2)
    
    # 初始化用户反馈数据
    analytics_dir = data_dir / "analytics"
    analytics_dir.mkdir(exist_ok=True)
    
    # 创建空的反馈文件
    feedback_file = analytics_dir / "user_feedback.json"
    if not feedback_file.exists():
        with open(feedback_file, 'w', encoding='utf-8') as f:
            json.dump([], f)
    
    # 创建空的权重文件
    weights_file = analytics_dir / "rule_weights.json"
    if not weights_file.exists():
        with open(weights_file, 'w', encoding='utf-8') as f:
            json.dump({}, f)
    
    # 添加一些示例反馈数据
    sample_feedback = [
        {
            "rule_id": "fire_door_width_demo",
            "feedback_type": "useful",
            "timestamp": datetime.now().isoformat(),
            "user_context": {"user_type": "architect", "experience": "5_years"}
        },
        {
            "rule_id": "office_height_demo",
            "feedback_type": "useful",
            "timestamp": datetime.now().isoformat(),
            "user_context": {"user_type": "junior_designer", "experience": "1_year"}
        },
        {
            "rule_id": "corridor_width_demo",
            "feedback_type": "false_positive",
            "timestamp": datetime.now().isoformat(),
            "user_context": {"user_type": "expert", "experience": "10_years"}
        }
    ]
    
    with open(feedback_file, 'w', encoding='utf-8') as f:
        json.dump(sample_feedback, f, ensure_ascii=False, indent=2)
    
    # 初始化示例权重
    sample_weights = {
        "fire_door_width_demo": {"confidence": 1.5, "usage_count": 12},
        "office_height_demo": {"confidence": 1.2, "usage_count": 8},
        "corridor_width_demo": {"confidence": 0.7, "usage_count": 5}
    }
    
    with open(weights_file, 'w', encoding='utf-8') as f:
        json.dump(sample_weights, f, ensure_ascii=False, indent=2)
    
    print("✅ 演示数据初始化完成！")
    print(f"📁 规则文件: {rules_file}")
    print(f"📊 反馈数据: {feedback_file}")
    print(f"⚖️ 权重数据: {weights_file}")
    print("\n🚀 现在可以访问：")
    print("   API文档: http://localhost:8080/docs")
    print("   智能演示: http://localhost:8080/smart/")

if __name__ == "__main__":
    init_demo_data()